package aor2.components;

public class AlushiftCircuit {

	private AlushiftOperation operation;
	protected Element out;
	protected Element X, Y;

	public AlushiftCircuit(Element out, Element x, Element y) {
		super();
		this.out = out;
		X = x;
		Y = y;
		operation = AlushiftOperation.nop;
	}

	protected AlushiftCircuit() {
		super();
		operation = AlushiftOperation.nop;
	}

	public void clearOperation() {
		operation = AlushiftOperation.nop;
	}

	public void setOperation(AlushiftOperation operation) {
		this.operation = operation;
	}

	public AlushiftOperation getOperation() {
		return operation;
	}

	public void out() {
		if (out == null) {
			System.out.println("ALUSHIFT: izlaz nije povezan");
			return;
		}
		out.setState(getState());
	}

	protected long getState() {
		long output = 0;
		switch (operation) {
		case add: {
			output = add();
			break;
		}
		case sub: {
			output = sub();
			break;
		}
		case or: {
			output = or();
			break;
		}
		case inc: {
			output = inc();
			break;
		}
		case dec: {
			output = dec();
			break;
		}
		case not: {
			output = not();
			break;
		}
		case shl: {
			output = shl();
			break;
		}
		case asr: {
			output = asr();
			break;
		}
		case sgnext: {
			output = sgnext();
			break;
		}
		case trans: {
			output = trans();
			break;
		}
		case nop: {
			output = nop();
			break;
		}
		}
		return output;
	}

	private long sgnext() {
		long bits;
		long x = X.getState();
		if ((x & 0x0080L) != 0)
			bits = 0xFFFFFFFFFFFFFF00L;
		else
			bits = 0;
		return x | bits;
	}

	private long nop() {
		return Element.UNDEFINED;
	}

	private long trans() {
		if (X == null) {
			System.out.println("ALUSHIFT: ulaz X nije povezan");
			return 0;
		}
		return X.getState();
	}

	private long asr() {
		if (X == null) {
			System.out.println("ALUSHIFT: ulaz X nije povezan");
			return 0;
		}
		return X.getState() / 2;
	}

	private long shl() {
		if (X == null) {
			System.out.println("ALUSHIFT: ulaz X nije povezan");
			return 0;
		}
		return X.getState() * 2;
	}

	private long not() {
		if (X == null) {
			System.out.println("ALUSHIFT: ulaz X nije povezan");
			return 0;
		}
		return ~X.getState();
	}

	private long dec() {
		if (X == null) {
			System.out.println("ALUSHIFT: ulaz X nije povezan");
			return 0;
		}
		return X.getState() - 1;
	}

	private long inc() {
		if (X == null) {
			System.out.println("ALUSHIFT: ulaz X nije povezan");
			return 0;
		}
		return X.getState() + 1;
	}

	private long or() {
		if (X == null) {
			System.out.println("ALUSHIFT: ulaz X nije povezan");
			return 0;
		}
		if (Y == null) {
			System.out.println("ALUSHIFT: ulaz Y nije povezan");
			return 0;
		}
		return X.getState() | Y.getState();
	}

	private long sub() {
		if (X == null) {
			System.out.println("ALUSHIFT: ulaz X nije povezan");
			return 0;
		}
		if (Y == null) {
			System.out.println("ALUSHIFT: ulaz Y nije povezan");
			return 0;
		}
		return X.getState() - Y.getState();
	}

	private long add() {
		if (X == null) {
			System.out.println("ALUSHIFT: ulaz X nije povezan");
			return 0;
		}
		if (Y == null) {
			System.out.println("ALUSHIFT: ulaz Y nije povezan");
			return 0;
		}
		return X.getState() + Y.getState();
	}

}
